home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zfproc.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  11.5 KB  |  390 lines

  1. /* Copyright (C) 1994, 1995, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zfproc.c,v 1.4.2.4 2000/12/02 19:55:31 raph Exp $ */
  20. /* Procedure-based filter stream support */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "oper.h"               /* for ifilter.h */
  24. #include "estack.h"
  25. #include "gsstruct.h"
  26. #include "ialloc.h"
  27. #include "istruct.h"            /* for RELOC_REF_VAR */
  28. #include "stream.h"
  29. #include "strimpl.h"
  30. #include "ifilter.h"
  31. #include "files.h"
  32. #include "store.h"
  33.  
  34. /* ---------------- Generic ---------------- */
  35.  
  36. /* GC procedures */
  37. private 
  38. CLEAR_MARKS_PROC(sproc_clear_marks)
  39. {
  40.     stream_proc_state *const pptr = vptr;
  41.  
  42.     r_clear_attrs(&pptr->proc, l_mark);
  43.     r_clear_attrs(&pptr->data, l_mark);
  44. }
  45. private 
  46. ENUM_PTRS_WITH(sproc_enum_ptrs, stream_proc_state *pptr) return 0;
  47. case 0:
  48. ENUM_RETURN_REF(&pptr->proc);
  49. case 1:
  50. ENUM_RETURN_REF(&pptr->data);
  51. ENUM_PTRS_END
  52. private RELOC_PTRS_WITH(sproc_reloc_ptrs, stream_proc_state *pptr);
  53. RELOC_REF_VAR(pptr->proc);
  54. r_clear_attrs(&pptr->proc, l_mark);
  55. RELOC_REF_VAR(pptr->data);
  56. r_clear_attrs(&pptr->data, l_mark);
  57. RELOC_PTRS_END
  58.  
  59. /* Structure type for procedure-based streams. */
  60. private_st_stream_proc_state();
  61.  
  62. /* Allocate and open a procedure-based filter. */
  63. /* The caller must have checked that *sop is a procedure. */
  64. private int
  65. s_proc_init(ref * sop, stream ** psstrm, uint mode,
  66.             const stream_template * temp, const stream_procs * procs,
  67.             gs_ref_memory_t *imem)
  68. {
  69.     gs_memory_t *const mem = (gs_memory_t *)imem;
  70.     stream *sstrm = file_alloc_stream(mem, "s_proc_init(stream)");
  71.     stream_proc_state *state = (stream_proc_state *)
  72.         s_alloc_state(mem, &st_sproc_state, "s_proc_init(state)");
  73.  
  74.     if (sstrm == 0 || state == 0) {
  75.         gs_free_object(mem, state, "s_proc_init(state)");
  76.         /*gs_free_object(mem, sstrm, "s_proc_init(stream)"); *//* just leave it on the file list */
  77.         return_error(e_VMerror);
  78.     }
  79.     s_std_init(sstrm, NULL, 0, procs, mode);
  80.     sstrm->procs.process = temp->process;
  81.     state->template = temp;
  82.     state->memory = mem;
  83.     state->min_left = 1;
  84.     state->eof = 0;
  85.     state->proc = *sop;
  86.     make_empty_string(&state->data, a_all);
  87.     state->index = 0;
  88.     sstrm->state = (stream_state *) state;
  89.     *psstrm = sstrm;
  90.     return 0;
  91. }
  92.  
  93. /* Handle an interrupt during a stream operation. */
  94. /* This is logically unrelated to procedure streams, */
  95. /* but it is also associated with the interpreter stream machinery. */
  96. private int
  97. s_handle_intc(i_ctx_t *i_ctx_p, const ref *pstate, int nstate,
  98.               op_proc_t cont)
  99. {
  100.     int npush = nstate + 2;
  101.  
  102.     check_estack(npush);
  103.     if (nstate)
  104.         memcpy(esp + 2, pstate, nstate * sizeof(ref));
  105. #if 0                           /* **************** */
  106.     {
  107.         int code = gs_interpret_error(e_interrupt, (ref *) (esp + npush));
  108.  
  109.         if (code < 0)
  110.             return code;
  111.     }
  112. #else /* **************** */
  113.     npush--;
  114. #endif /* **************** */
  115.     make_op_estack(esp + 1, cont);
  116.     esp += npush;
  117.     return o_push_estack;
  118. }
  119.  
  120. /* Set default parameter values (actually, just clear pointers). */
  121. private void
  122. s_proc_set_defaults(stream_state * st)
  123. {
  124.     stream_proc_state *const ss = (stream_proc_state *) st;
  125.  
  126.     make_null(&ss->proc);
  127.     make_null(&ss->data);
  128. }
  129.  
  130. /* ---------------- Read streams ---------------- */
  131.  
  132. /* Forward references */
  133. private stream_proc_process(s_proc_read_process);
  134. private int s_proc_read_continue(P1(i_ctx_t *));
  135.  
  136. /* Stream templates */
  137. private const stream_template s_proc_read_template = {
  138.     &st_sproc_state, NULL, s_proc_read_process, 1, 1,
  139.     NULL, s_proc_set_defaults
  140. };
  141. private const stream_procs s_proc_read_procs = {
  142.     s_std_noavailable, s_std_noseek, s_std_read_reset,
  143.     s_std_read_flush, s_std_null, NULL
  144. };
  145.  
  146. /* Allocate and open a procedure-based read stream. */
  147. /* The caller must have checked that *sop is a procedure. */
  148. int
  149. sread_proc(ref * sop, stream ** psstrm, gs_ref_memory_t *imem)
  150. {
  151.     int code =
  152.         s_proc_init(sop, psstrm, s_mode_read, &s_proc_read_template,
  153.                     &s_proc_read_procs, imem);
  154.  
  155.     if (code < 0)
  156.         return code;
  157.     (*psstrm)->end_status = CALLC;
  158.     return code;
  159. }
  160.  
  161. /* Handle an input request. */
  162. private int
  163. s_proc_read_process(stream_state * st, stream_cursor_read * ignore_pr,
  164.                     stream_cursor_write * pw, bool last)
  165. {
  166.     /* Move data from the string returned by the procedure */
  167.     /* into the stream buffer, or ask for a callback. */
  168.     stream_proc_state *const ss = (stream_proc_state *) st;
  169.     uint count = r_size(&ss->data) - ss->index;
  170.  
  171.     if (count > 0) {
  172.         uint wcount = pw->limit - pw->ptr;
  173.  
  174.         if (wcount < count)
  175.             count = wcount;
  176.         memcpy(pw->ptr + 1, ss->data.value.bytes + ss->index, count);
  177.         pw->ptr += count;
  178.         ss->index += count;
  179.         return 1;
  180.     }
  181.     return (ss->eof ? EOFC : CALLC);
  182. }
  183.  
  184. /* Handle an exception (INTC or CALLC) from a read stream */
  185. /* whose buffer is empty. */
  186. int
  187. s_handle_read_exception(i_ctx_t *i_ctx_p, int status, const ref * fop,
  188.                         const ref * pstate, int nstate, op_proc_t cont)
  189. {
  190.     int npush = nstate + 4;
  191.     stream *ps;
  192.  
  193.     switch (status) {
  194.         case INTC:
  195.             return s_handle_intc(i_ctx_p, pstate, nstate, cont);
  196.         case CALLC:
  197.             break;
  198.         default:
  199.             return_error(e_ioerror);
  200.     }
  201.     /* Find the stream whose buffer needs refilling. */
  202.     for (ps = fptr(fop); ps->strm != 0;)
  203.         ps = ps->strm;
  204.     check_estack(npush);
  205.     if (nstate)
  206.         memcpy(esp + 2, pstate, nstate * sizeof(ref));
  207.     make_op_estack(esp + 1, cont);
  208.     esp += npush;
  209.     make_op_estack(esp - 2, s_proc_read_continue);
  210.     esp[-1] = *fop;
  211.     r_clear_attrs(esp - 1, a_executable);
  212.     *esp = ((stream_proc_state *) ps->state)->proc;
  213.     return o_push_estack;
  214. }
  215. /* Continue a read operation after returning from a procedure callout. */
  216. /* osp[0] contains the file (pushed on the e-stack by handle_read_status); */
  217. /* osp[-1] contains the new data string (pushed by the procedure). */
  218. /* The top of the e-stack contains the real continuation. */
  219. private int
  220. s_proc_read_continue(i_ctx_t *i_ctx_p)
  221. {
  222.     os_ptr op = osp;
  223.     os_ptr opbuf = op - 1;
  224.     stream *ps;
  225.     stream_proc_state *ss;
  226.  
  227.     check_file(ps, op);
  228.     check_read_type(*opbuf, t_string);
  229.     while ((ps->end_status = 0, ps->strm) != 0)
  230.         ps = ps->strm;
  231.     ss = (stream_proc_state *) ps->state;
  232.     ss->data = *opbuf;
  233.     ss->index = 0;
  234.     if (r_size(opbuf) == 0)
  235.         ss->eof = true;
  236.     pop(2);
  237.     return 0;
  238. }
  239.  
  240. /* ---------------- Write streams ---------------- */
  241.  
  242. /* Forward references */
  243. private stream_proc_flush(s_proc_write_flush);
  244. private stream_proc_process(s_proc_write_process);
  245. private int s_proc_write_continue(P1(i_ctx_t *));
  246.  
  247. /* Stream templates */
  248. private const stream_template s_proc_write_template = {
  249.     &st_sproc_state, NULL, s_proc_write_process, 1, 1,
  250.     NULL, s_proc_set_defaults
  251. };
  252. private const stream_procs s_proc_write_procs = {
  253.     s_std_noavailable, s_std_noseek, s_std_write_reset,
  254.     s_proc_write_flush, s_std_null, NULL
  255. };
  256.  
  257. /* Allocate and open a procedure-based write stream. */
  258. /* The caller must have checked that *sop is a procedure. */
  259. int
  260. swrite_proc(ref * sop, stream ** psstrm, gs_ref_memory_t *imem)
  261. {
  262.     return s_proc_init(sop, psstrm, s_mode_write, &s_proc_write_template,
  263.                        &s_proc_write_procs, imem);
  264. }
  265.  
  266. /* Handle an output request. */
  267. private int
  268. s_proc_write_process(stream_state * st, stream_cursor_read * pr,
  269.                      stream_cursor_write * ignore_pw, bool last)
  270. {
  271.     /* Move data from the stream buffer to the string */
  272.     /* returned by the procedure, or ask for a callback. */
  273.     stream_proc_state *const ss = (stream_proc_state *) st;
  274.     uint rcount = pr->limit - pr->ptr;
  275.  
  276.     if (rcount > 0) {
  277.         uint wcount = r_size(&ss->data) - ss->index;
  278.         uint count = min(rcount, wcount);
  279.  
  280.         memcpy(ss->data.value.bytes + ss->index, pr->ptr + 1, count);
  281.         pr->ptr += count;
  282.         ss->index += count;
  283.         if (rcount > wcount)
  284.             return CALLC;
  285.         else if (last) {
  286.             ss->eof = true;
  287.             return CALLC;
  288.         } else
  289.             return 0;
  290.     }
  291.     return ((ss->eof = last) ? EOFC : 0);
  292. }
  293.  
  294. /* Flush the output.  This is non-standard because it must call the */
  295. /* procedure. */
  296. private int
  297. s_proc_write_flush(stream *s)
  298. {
  299.     int result = s_process_write_buf(s, false);
  300.     stream_proc_state *const ss = (stream_proc_state *)s->state;
  301.  
  302.     return (result < 0 || ss->index == 0 ? result : CALLC);
  303. }
  304.  
  305. /* Handle an exception (INTC or CALLC) from a write stream */
  306. /* whose buffer is full. */
  307. int
  308. s_handle_write_exception(i_ctx_t *i_ctx_p, int status, const ref * fop,
  309.                          const ref * pstate, int nstate, op_proc_t cont)
  310. {
  311.     stream *ps;
  312.     stream_proc_state *psst;
  313.  
  314.     switch (status) {
  315.         case INTC:
  316.             return s_handle_intc(i_ctx_p, pstate, nstate, cont);
  317.         case CALLC:
  318.             break;
  319.         default:
  320.             return_error(e_ioerror);
  321.     }
  322.     /* Find the stream whose buffer needs emptying. */
  323.     for (ps = fptr(fop); ps->strm != 0;)
  324.         ps = ps->strm;
  325.     psst = (stream_proc_state *) ps->state;
  326.     {
  327.         int npush = nstate + 6;
  328.  
  329.         check_estack(npush);
  330.         if (nstate)
  331.             memcpy(esp + 2, pstate, nstate * sizeof(ref));
  332.         make_op_estack(esp + 1, cont);
  333.         esp += npush;
  334.         make_op_estack(esp - 4, s_proc_write_continue);
  335.         esp[-3] = *fop;
  336.         r_clear_attrs(esp - 3, a_executable);
  337.         make_bool(esp - 1, !psst->eof);
  338.     }
  339.     esp[-2] = psst->proc;
  340.     *esp = psst->data;
  341.     r_set_size(esp, psst->index);
  342.     return o_push_estack;
  343. }
  344. /* Continue a write operation after returning from a procedure callout. */
  345. /* osp[0] contains the file (pushed on the e-stack by handle_write_status); */
  346. /* osp[-1] contains the new buffer string (pushed by the procedure). */
  347. /* The top of the e-stack contains the real continuation. */
  348. private int
  349. s_proc_write_continue(i_ctx_t *i_ctx_p)
  350. {
  351.     os_ptr op = osp;
  352.     os_ptr opbuf = op - 1;
  353.     stream *ps;
  354.     stream_proc_state *ss;
  355.  
  356.     check_file(ps, op);
  357.     check_write_type(*opbuf, t_string);
  358.     while (ps->strm != 0) {
  359.     if (ps->end_status == CALLC)
  360.         ps->end_status = 0;
  361.     ps = ps->strm;
  362.     }
  363.     ps->end_status = 0;
  364.     ss = (stream_proc_state *) ps->state;
  365.     ss->data = *opbuf;
  366.     ss->index = 0;
  367.     pop(2);
  368.     return 0;
  369. }
  370.  
  371. /* ------ More generic ------ */
  372.  
  373. /* Test whether a stream is procedure-based. */
  374. bool
  375. s_is_proc(const stream *s)
  376. {
  377.     return (s->procs.process == s_proc_read_process ||
  378.             s->procs.process == s_proc_write_process);
  379. }
  380.  
  381. /* ------ Initialization procedure ------ */
  382.  
  383. const op_def zfproc_op_defs[] =
  384. {
  385.                 /* Internal operators */
  386.     {"2%s_proc_read_continue", s_proc_read_continue},
  387.     {"2%s_proc_write_continue", s_proc_write_continue},
  388.     op_def_end(0)
  389. };
  390.